package com.example.administrator.daydayappmarket.search.fragment;


/**
 *     TODO  搜索第一级页面
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.search.adapter.SearchFragmentFirst1Adapter;
import com.example.administrator.daydayappmarket.search.moduls.SearchActivityApp;
import com.example.administrator.daydayappmarket.search.moduls.SearchActivityParent;
import com.example.administrator.daydayappmarket.search.moduls.SearchActivityTitle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchFirstFragment extends Fragment {

    //数据源
    private List<List<SearchActivityParent>> dataList;
    //fragment显示的视图
    private View view;
    private ListView listView;
    private RequestQueue requestQueue;
    private SearchFragmentFirst1Adapter adapter;


    public SearchFirstFragment() {
        // Required empty public constructor

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataList = new ArrayList<>();
        //添加适配器
        adapter = new SearchFragmentFirst1Adapter(getContext(),dataList);

        //网络请求数据
        requestQueue = Volley.newRequestQueue(getContext());
        StringRequest stringRequest = new StringRequest(Contacts.SEARCH_MAIN, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String theme_type = jsonObject.getString("theme_type");
                        if(theme_type.equals("s_hotword")){
                            JSONArray data = jsonObject.getJSONArray("data");
                            List<SearchActivityParent> parents = new ArrayList<>();
                            for (int i1 = 0; i1 < data.length(); i1++) {

                                JSONObject jsonObject1 = data.getJSONObject(i1);
                                SearchActivityTitle title = new SearchActivityTitle();
                                title.setTitle(jsonObject1.getString("title"));
                                title.setTheme_type(theme_type);
                                parents.add(title);
                            }
                            dataList.add(parents);
                        }

                        else{
                            JSONArray data = jsonObject.getJSONArray("data");
                            List<SearchActivityParent> parents = new ArrayList<>();
                            for (int i1 = 0; i1 < data.length(); i1++) {
                                SearchActivityApp activityApp = new SearchActivityApp();
                                JSONObject jsonObject1 = data.getJSONObject(i1);
                                activityApp.setTheme_type(theme_type);
                                activityApp.setTitle(jsonObject1.getString("title"));
                                activityApp.JsonParse(jsonObject1);
                                parents.add(activityApp);
                            }
                            dataList.add(parents);

                        }

                    }

                    adapter.notifyDataSetChanged();


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        stringRequest.setTag("search_main");
        requestQueue.add(stringRequest);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        //创建视图
        view = inflater.inflate(R.layout.fragment_search_first_1, container, false);
        listView = (ListView) view.findViewById(R.id.fragment_search_first_1_listView);

        listView.setAdapter(adapter);

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("search_main");
    }
}
